<?php

	Class Yomm_File {
		
		static public function move($from_path, $to_path, $over_write = false)
		{
			if(file_exists($to_path) && $over_write === false)
			{
				throw new Yomm_Error("the file '{$to_path}' is already existing and over_write is false");
				return false;
			}
			
			$old_umask = umask(0);
			rename($from_path, $to_path);
        	@chmod($to_path, Yomm_Config::VAR_FILE_PERMISSION);
        	umask($old_umask);
        	
        	return;
		}
		
		static public function copy($from_path, $to_path, $over_write = false)
		{
			if(file_exists($to_path) && $over_write === false)
			{
				throw new Yomm_Error("the file '{$to_path}' is already existing and over_write is false");
				return false;
			}
			
			$old_umask = umask(0);
			copy($from_path, $to_path);
        	@chmod($to_path, Yomm_Config::VAR_FILE_PERMISSION);
        	umask($old_umask);
        	
        	return;
		}
		
		static public function create($file_save_path, $file_content)
		{
			$old_umask = umask(0);
			
			$_tmp_file = tempnam(Yomm_Config::DIR_CACHE, 'tplfilecache');
			
			if(!file_put_contents($_tmp_file, $file_content))
			{
				umask($old_umask);
				throw new Yomm_Error("unable to write content into file {$_tmp_file}");
				return false;
			}
			
			if(file_exists($file_save_path))
			{
				unlink($file_save_path);
			}
			
			if(@rename($_tmp_file, $file_save_path) === false)
			{
				unlink($_tmp_file);
				throw new Yomm_Error("unable to rename {$_tmp_file} into {$file_save_path}");
				return false;	
			}
			
			// set the new permissions
			chmod($file_save_path, Yomm_Config::VAR_FILE_PERMISSION);
			umask($old_umask);
			
			return true;
		}
		
		static public function getContent($file)
		{
			return file_get_contents($file);
		}
		
	}